package com.ruoyi.report_hntjg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.report_gc.domain.bo.GcjxljjtbgBsInfoBo;
import com.ruoyi.report_hntjg.domain.bo.ReportHntjgGjbhcbgBsInfoBo;
import com.ruoyi.report_hntjg.mapper.ReportHntjgGjbhcbgBaDetailMapper;
import com.ruoyi.report_hntjg.mapper.ReportHntjgGjbhcbgBsInfoMapper;
import com.ruoyi.report_hntjg.service.IReportHntjgGjbhcbgBsInfoService;
import com.ruoyi.report_hntjg.domain.ReportHntjgGjbhcbgBaDetail;
import com.ruoyi.report_hntjg.domain.ReportHntjgGjbhcbgBsInfo;
import com.ruoyi.report_hntjg.domain.vo.ReportHntjgGjbhcbgBaDetailVo;
import com.ruoyi.report_hntjg.domain.vo.ReportHntjgGjbhcbgBsInfoVo;
import com.ruoyi.report_tg.domain.vo.TgsybgBaDetailVo;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 钢筋位置、保护层厚度试验检测报告Service业务层处理
 *
 * @author tll
 * @date 2024-08-07
 */
@RequiredArgsConstructor
@Service
public class ReportHntjgGjbhcbgBsInfoServiceImpl implements IReportHntjgGjbhcbgBsInfoService {

    private final ReportHntjgGjbhcbgBsInfoMapper baseMapper;
    private final ReportHntjgGjbhcbgBaDetailMapper reportHntjgGjbhcbgBaDetailMapper;

    /**
     * 查询钢筋位置、保护层厚度试验检测报告
     */
    @Override
    @FIllBasicInfo
    public ReportHntjgGjbhcbgBsInfoVo queryById(Long excelId){
        // 主表
        ReportHntjgGjbhcbgBsInfoVo vo = baseMapper.selectVoById(excelId);
        // 子表
        LambdaQueryWrapper<ReportHntjgGjbhcbgBaDetail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(excelId != null, ReportHntjgGjbhcbgBaDetail::getExcelId, excelId);
        lqw.orderByAsc(ReportHntjgGjbhcbgBaDetail::getCreateTime);
        lqw.orderByAsc(ReportHntjgGjbhcbgBaDetail::getSort);
        List<ReportHntjgGjbhcbgBaDetailVo> detailList = reportHntjgGjbhcbgBaDetailMapper.selectVoList(lqw);
        vo.setDetailList(detailList);
        return vo;
    }

    /**
     * 查询钢筋位置、保护层厚度试验检测报告列表
     */
    @Override
    public TableDataInfo<ReportHntjgGjbhcbgBsInfoVo> queryPageList(ReportHntjgGjbhcbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportHntjgGjbhcbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportHntjgGjbhcbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询钢筋位置、保护层厚度试验检测报告列表
     */
    @Override
    public List<ReportHntjgGjbhcbgBsInfoVo> queryList(ReportHntjgGjbhcbgBsInfoBo bo) {
        LambdaQueryWrapper<ReportHntjgGjbhcbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportHntjgGjbhcbgBsInfo> buildQueryWrapper(ReportHntjgGjbhcbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportHntjgGjbhcbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportHntjgGjbhcbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportHntjgGjbhcbgBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportHntjgGjbhcbgBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportHntjgGjbhcbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportHntjgGjbhcbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportHntjgGjbhcbgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportHntjgGjbhcbgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportHntjgGjbhcbgBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportHntjgGjbhcbgBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getYangpsj() != null, ReportHntjgGjbhcbgBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportHntjgGjbhcbgBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportHntjgGjbhcbgBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportHntjgGjbhcbgBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportHntjgGjbhcbgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportHntjgGjbhcbgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportHntjgGjbhcbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportHntjgGjbhcbgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobmc()), ReportHntjgGjbhcbgBsInfo::getBaobmc, bo.getBaobmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobbh()), ReportHntjgGjbhcbgBsInfo::getBaobbh, bo.getBaobbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportHntjgGjbhcbgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), ReportHntjgGjbhcbgBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), ReportHntjgGjbhcbgBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancr()), ReportHntjgGjbhcbgBsInfo::getJiancr, bo.getJiancr());
        lqw.eq(StringUtils.isNotBlank(bo.getJilr()), ReportHntjgGjbhcbgBsInfo::getJilr, bo.getJilr());
        lqw.eq(StringUtils.isNotBlank(bo.getFuhr()), ReportHntjgGjbhcbgBsInfo::getFuhr, bo.getFuhr());
        lqw.eq(bo.getRiq() != null, ReportHntjgGjbhcbgBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getQuydd()), ReportHntjgGjbhcbgBsInfo::getQuydd, bo.getQuydd());
        lqw.eq(bo.getJincrq() != null, ReportHntjgGjbhcbgBsInfo::getJincrq, bo.getJincrq());
        lqw.eq(bo.getDaibsl() != null, ReportHntjgGjbhcbgBsInfo::getDaibsl, bo.getDaibsl());
        lqw.eq(StringUtils.isNotBlank(bo.getChand()), ReportHntjgGjbhcbgBsInfo::getChand, bo.getChand());
        lqw.eq(bo.getRefExcelId() != null, ReportHntjgGjbhcbgBsInfo::getRefExcelId, bo.getRefExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportHntjgGjbhcbgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), ReportHntjgGjbhcbgBsInfo::getBgFlag, bo.getBgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportHntjgGjbhcbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportHntjgGjbhcbgBsInfo::getPandyjIds, bo.getPandyjIds());
        return lqw;
    }

    /**
     * 新增钢筋位置、保护层厚度试验检测报告
     */
    @Override
    public Boolean insertByBo(ReportHntjgGjbhcbgBsInfoBo bo) {
        ReportHntjgGjbhcbgBsInfo add = BeanUtil.toBean(bo, ReportHntjgGjbhcbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改钢筋位置、保护层厚度试验检测报告
     */
    @Override
    public Boolean updateByBo(ReportHntjgGjbhcbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        // 主表
        ReportHntjgGjbhcbgBsInfo update = BeanUtil.toBean(bo, ReportHntjgGjbhcbgBsInfo.class);
        validEntityBeforeSave(update);
        boolean upd = baseMapper.updateById(update) > 0;
        // 子表
        boolean upd2 = false;
        boolean insert = false;
        if (ObjectUtils.isNotEmpty(bo.getDetailList())) {
            // 根据excelId查询明细
            QueryWrapper<ReportHntjgGjbhcbgBaDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("excel_id", bo.getExcelId());
            List<ReportHntjgGjbhcbgBaDetail> list = reportHntjgGjbhcbgBaDetailMapper.selectList(queryWrapper);
            for (ReportHntjgGjbhcbgBaDetail det : bo.getDetailList()) {
                if (ObjectUtils.isEmpty(list)) {
                    // 新增
                    insert = reportHntjgGjbhcbgBaDetailMapper.insert(det) > 0;
                } else if (ObjectUtils.isNotEmpty(det.getDetailId())) {
                    // 修改
                    upd2 = reportHntjgGjbhcbgBaDetailMapper.updateById(det) > 0;
                } else {
                    throw new ServiceException("更新主键不能为空, 请刷新后重试!");
                }
            }
        }
        return upd && upd2 && insert;
    }

    private void fillExcelResult(ReportHntjgGjbhcbgBsInfoBo bo) {
//        if (CollUtil.isNotEmpty(bo.getBaDetailVos())) {
//            for (TgsybgBaDetailVo detailVo : bo.getBaDetailVos()) {
//                if ("不符合".equals(detailVo.getJiegpd())) {
//                    bo.setExcelResult(2);
//                    return;
//                }
//            }
//            bo.setExcelResult(1);
//        }
        bo.setExcelResult(1);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportHntjgGjbhcbgBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除钢筋位置、保护层厚度试验检测报告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
